<?php

/**
 * Met en place les éléments de navigation
 *
 * @author Cyril Souti
 * @version 1.0
 * @package plugins
 *
 */
class Plugins_NavigationPlugin extends Zend_Controller_Plugin_Abstract
{
    /**
     * (non-PHPdoc)
     * @see Zend/Controller/Plugin/Zend_Controller_Plugin_Abstract#dispatchLoopStartup($request)
     */
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        $navigation = new Zend_Navigation();
        $navigation->addPages($this->getPages($request));
        Manager::setNavigation($navigation);
    }

    public function getPages(Zend_Controller_Request_Abstract $request)
    {
        $keyCachePages = md5(implode("", $request->getParams()));
        $nav = $this;
        return Manager::loadCache($keyCachePages, function () use ($nav, $request) {
            $tableRubrique = Manager::getFactoryModel()->getRubrique();
            $rubriquesRacine = $tableRubrique->getRubriquesRacinesActives();
            $pages = array();
            foreach (Manager::getTranslate()->getList() as $lang) {
                foreach ($rubriquesRacine as $rubrique) {
                    $page = $nav->getPage($rubrique, $request, $lang);
                    array_push($pages, $page);
                }
            }
            return $pages;
        });
    }

    /**
     * Retourne un tableau correspondant à la navigation
     * de la rubrique donné
     *
     * @param Zend_Db_Table_Row $rubrique une rubrique
     * @param Zend_Controller_Request_Abstract $request une requête
     * @return array(string => string) la navigation de la rubrique
     */
    public function getPage(Zend_Db_Table_Row $rubrique, Zend_Controller_Request_Abstract $request, $lang)
    {
        $translate = Manager::getTranslate();
        $page = array();
        $page['route'] = Plugins_InternationalisationPlugin::ROUTE_LANGAGE;
        $page['controller'] = $rubrique->controller;
        $page['action'] = $rubrique->action;
        $page['module'] = $rubrique->module;
        $page['label'] = $translate->translate($rubrique->label);
        $page['title'] = $translate->translate($rubrique->titre);
        $page['class'] = 'item' . $rubrique->position;
        $page['params'] = array(Plugins_InternationalisationPlugin::ROUTE_LANGAGE => $lang);
        $page['active'] = $this->rubriqueIsActive($rubrique, $request);
        $page['order'] = $rubrique->position;
        $page['pages'] = array();

        if (strcasecmp(Plugins_PhotosPlugin::CONTROLLER_PHOTOS_DEFAULT, $rubrique->controller) == 0) {
            $photoPlugin = Zend_Controller_Front::getInstance()->getPlugin('Plugins_InitialisationPlugin')->getAvailablePlugin("photos");
            $order = 0;
            foreach ($photoPlugin->getTags() as $tag) {
                array_push($page['pages'], $this->getPagePhoto($tag, $lang, $order));
                $order++;
            }
        } else {
            $tableRubrique = Manager::getFactoryModel()->getRubrique();
            $rubriquesFilles = $tableRubrique->getRubriqueFilleActivesFromRubrique($rubrique);
            foreach ($rubriquesFilles as $rubriqueFille) {
                array_push($page['pages'], $this->getPage($rubriqueFille, $request, $lang));
            }
        }
        return $page;
    }

    public function getPagePhoto($tag, $lang, $order)
    {
        $currentTagLabel = Zend_Controller_Front::getInstance()->getRequest()->getParam(Plugins_PhotosPlugin::PARAMETRE_TAG);
        $translate = Manager::getTranslate();

        $page = array();
        $page['route'] = Plugins_PhotosPlugin::ROUTE_PHOTOS;
        $page['label'] = $translate->translate($tag->titre);
        $page['title'] = $translate->translate($tag->titre);
        $page['class'] = 'photoTag firstletter size'.$tag->poids;
        $page['params'] = array(Plugins_InternationalisationPlugin::ROUTE_LANGAGE => $lang, Plugins_PhotosPlugin::PARAMETRE_TAG => $tag->label);
        $page['active'] = strcasecmp($tag->label, $currentTagLabel) == 0;
        $page['order'] = $order;
        $page['pages'] = array();

        return $page;
    }

    /**
     * Détermine si la rubrique donnée est active
     *
     * @param Zend_Db_Table_Row $rubrique une rubrique
     * @param Zend_Controller_Request_Abstract $request une requête
     * @return boolean true si la rubrique est active false sinon
     */
    private function rubriqueIsActive(Zend_Db_Table_Row $rubrique, Zend_Controller_Request_Abstract $request)
    {
        return
            strcasecmp($rubrique->controller, $request->getParam("controller")) == 0;
        //		&& strcasecmp($rubrique->action, $request->getParam("action")) == 0
        //		&& strcasecmp($rubrique->module, $request->getParam("module")) == 0;
    }
}